﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Clm;

namespace FIMCMTools.Providers
{
    public class CustomSecretProvider : ISecretProvider
    {
        private readonly ASCIIEncoding encoding;
        public byte[] CalculateHash(string[] secrets)
        {
            if (secrets != null)
            {
                byte[] numArray = null;
                MemoryStream memoryStream = new MemoryStream(100);
                int length = 0;
                for (int i = 0; i < (int)secrets.Length; i++)
                {
                    byte[] bytes = this.encoding.GetBytes(this.SanitizeSecret(secrets[i]));
                    memoryStream.Write(bytes, 0, (int)bytes.Length);
                    length = length + (int)bytes.Length;
                }
                memoryStream.SetLength((long)length);
                memoryStream.Seek((long)0, SeekOrigin.Begin);
                SHA1 sHA1 = SHA1.Create();
                numArray = sHA1.ComputeHash(memoryStream);
                memoryStream.Close();
                return numArray;
            }
            else
            {
                throw new ArgumentNullException("secrets");
            }
        }

        public TimeSpan ExpirationPeriod
        {
            get { throw new NotImplementedException(); }
        }

        public void GenerateSecrets(Microsoft.Clm.Common.Requests.Request request)
        {
            throw new NotImplementedException();
        }

        public DateTime GetExpirationDate(Microsoft.Clm.Common.Requests.Request request)
        {
            throw new NotImplementedException();
        }

        public string[] GetSecrets(Microsoft.Clm.Common.Requests.Request request)
        {
            throw new NotImplementedException();
        }

        public void Initialize(string data)
        {
           
        }

        public int NumberOfSecrets
        {
            get { return 1; }
        }

        public bool ValidateSecretIntegrity(string secret)
        {
            throw new NotImplementedException();
        }

        public bool ValidateUserSecrets(string[] secrets, Microsoft.Clm.Common.Requests.Request request, bool throwException)
        {
            throw new NotImplementedException();
        }

        #region Helpers
        protected virtual string SanitizeSecret(string secret)
        {
            string upper = null;
            if (secret != null)
            {
                upper = secret.Trim().ToUpper(CultureInfo.InvariantCulture);
                upper = upper.Replace(" ", "");
                upper = upper.Replace("-", "");
                upper = upper.Replace("O", "0");
                upper = upper.Replace("I", "1");
                upper = upper.Replace("L", "1");
                return upper;
            }
            else
            {
                return null;
            }
        }
        #endregion

    }
}
